package com.sh.data.engine.application.model.request.integration;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class TaskConfigRequest {
    // 目标数据源
    @NotNull
    @Schema(description = "目标数据源id", requiredMode = Schema.RequiredMode.REQUIRED)
    private Long targetDsId;

    // 目标数据源类型
    @NotNull
    @Schema(description = "目标数据源类型", requiredMode = Schema.RequiredMode.REQUIRED)
    private String targetDsType;
    // 目标表
    @NotNull
    @Schema(description = "目标表名", requiredMode = Schema.RequiredMode.REQUIRED)
    private String targetTableName;

    // 替换策略
    @Schema(description = "主键替换策略")
    private Integer dataReplaceRuleId;
    // 同步前清空历史记录
    @NotNull
    @Schema(description = "是否在同步前清空记录", requiredMode = Schema.RequiredMode.REQUIRED)
    private Integer isTruncate;

    // 失败记录阈值
    @NotNull
    @Schema(description = "失败次数阈值", requiredMode = Schema.RequiredMode.REQUIRED)
    private Integer errorNumLimit;

    @Schema(description = "字段映射方式", requiredMode = Schema.RequiredMode.REQUIRED)
    private Integer fieldMapType;

    // 字段映射
    @NotNull
    @Schema(description = "普通字段列表", requiredMode = Schema.RequiredMode.REQUIRED)
    private List<FieldMapping> fieldMappings;

    @Schema(description = "mongo hbase字段列表", requiredMode = Schema.RequiredMode.REQUIRED)
    private List<TargetField> targetFields;

    @Schema(description = "分区字段列表")
    private List<PartitionField> partitions;

    /*生效开始时间*/
    @Schema(description = "生效开始日期", requiredMode = Schema.RequiredMode.REQUIRED)
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
    private Date effectiveDateStart;

    /*生效结束时间*/
    @Schema(description = "生效结束日期", requiredMode = Schema.RequiredMode.REQUIRED)
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
    private Date effectiveDateEnd;

    /*调度周期 0-月 1-周 2-天 3-小时 4-分钟*/
    @Schema(description = "调度周期 0-月 1-周 2-天 3-小时 4-分钟", requiredMode = Schema.RequiredMode.REQUIRED)
    private Integer cycle;

    /*执行开始时间的小时*/
    @Schema(description = "执行时间的小时-开始", requiredMode = Schema.RequiredMode.REQUIRED)
    private String startTimeHour;

    /*执行开始时间的分钟*/
    @Schema(description = "执行时间的分钟-开始", requiredMode = Schema.RequiredMode.REQUIRED)
    private String startTimeMin;

    /*执行结束时间的小时*/
    @Schema(description = "执行结束时间的小时", requiredMode = Schema.RequiredMode.REQUIRED)
    private String endTimeHour;

    /*执行结束时间的分钟*/
    @Schema(description = "执行结束时间的分钟", requiredMode = Schema.RequiredMode.REQUIRED)
    private String endTimeMin;

    /*
     * 月、周为执行日期 ，小时、分钟为执行间隔
     */
    @Schema(
        description = "调度间隔, 选择周期为月时,显示1-30天, 星期时显示星期几. 周期为天时,不显示输入框, 周期为小时,显示1-23小时, 周期为分钟时,显示分钟",
        requiredMode = Schema.RequiredMode.REQUIRED)
    private String execPoint;

    /*跨周期依赖 0-不依赖上一周期 1-等待上一周期结束*/
    @Schema(description = "跨周期依赖", requiredMode = Schema.RequiredMode.REQUIRED)
    private Integer cycleDependence;

    @Data
    @Schema(description = "普通字段映射")
    @NoArgsConstructor
    @AllArgsConstructor
    public static class FieldMapping {
        @NotNull
        @Schema(description = "第二步中用户选择表的字段名", requiredMode = Schema.RequiredMode.REQUIRED)
        private String sourceField;

        @NotNull
        @Schema(description = "第二步中用户选择表的字段类型", requiredMode = Schema.RequiredMode.REQUIRED)
        private String sourceFieldType;

        @Schema(description = "该键是否是主键")
        private Integer isSourceFieldPk;

        @NotNull
        @Schema(description = "目标字段名", requiredMode = Schema.RequiredMode.REQUIRED)
        private String targetField;

        @NotNull
        @Schema(description = "目标字段类型", requiredMode = Schema.RequiredMode.REQUIRED)
        private String targetFieldType;

        @Schema(description = "是否是主键")
        private Integer isTargetFieldPk;

        @Schema(description = "目标字段描述")
        private String targetComment;
    }

    @Data
    @Schema(description = "mongo hbase字段映射")
    @NoArgsConstructor
    @AllArgsConstructor
    public static class TargetField {
        @NotNull
        @Schema(description = "第二步中用户选择表的字段名", requiredMode = Schema.RequiredMode.REQUIRED)
        private String fieldName;

        @NotNull
        @Schema(description = "第二步中用户选择表的字段类型", requiredMode = Schema.RequiredMode.REQUIRED)
        private String fieldType;
    }

    @Data
    @Schema(description = "分区字段")
    @NoArgsConstructor
    @AllArgsConstructor
    public static class PartitionField {

        @NotNull
        @Schema(description = "分区字段", requiredMode = Schema.RequiredMode.REQUIRED)
        private String fieldName;

        @NotNull
        @Schema(description = "分区字段值", requiredMode = Schema.RequiredMode.REQUIRED)
        private String fieldValue;
    }
}
